/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.helper;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.core.runtime.Assert;

import com.cea.papyrus.core.utils.Debug;

/**
 * Container for modeler factories.
 * 
 * @author Remi SCHNEKENBURGER
 */
public class ModelerHelperRegistry {
	
	/**
	 * Hash map that contains all diagram factories.
	 */
	private Map<String, IModelerHelper> map = new HashMap<String, IModelerHelper>(15);
	
	/**
	 * Register a factory in this registry.
	 * NOTA: the factory must have an identifier, used as a key to be retrived
	 * 
	 * @param factory the factory being registered
	 */
	public void registerFactory(IModelerHelper factory) {
		Assert.isNotNull(factory.getId());
		registerFactory(factory.getId(), factory);
	}

	/**
	 * Register a factory in this registry, using the given id.
	 * 
	 * @param factory  the factory being registered
	 * @param id the ID of the factory being registered
	 */
	public void registerFactory(String id, IModelerHelper factory) {
		map.put(id, factory);
	}
	
	/**
	 * Removes a factory from this registry. The factory must have an ID.
	 * 
	 * @param factory the factory to remove
	 */
	public void removeFactory(IModelerHelper factory) {
		Assert.isNotNull(factory.getId());
		map.remove(factory.getId());
	}

	/**
	 * Removes the factory with the given id from this registry.
	 * 
	 * @param id the id of the factory to remove
	 */
	public void removeFactory(String id) {
		map.remove(id);
	}
	
	/**
	 * Returns a {@link Collection}of registred factories.
	 * 
	 * @return the collection of registred factories
	 */
	public Collection<IModelerHelper> getFactories() {
		return map.values();
	}

	/**
	 * Returns the {@link IModelerHelper} with the given key or <code>null</code>.
	 * 
	 * To retrieve the modelerHelper for a specific diagram,
	 * use the static method : {@link ModelerHelper}::getFactory(TypeDiagram type) 
	 * instead
	 * 
	 * @param key the ID of the factory being requested
	 * 
	 * @return the factory with the corresponding ID of <code>null</code>
	 */
	public IModelerHelper getFactory(Object key) {
		return map.get(key);
	}
	
	/**
	 * Returns the {@link IModelerHelper} that is able to deal with the element.
	 * 
	 * @param elementClass the metaclass of the element to create or delete
	 * 
	 * @return the factory
	 */
	public IModelerHelper getFactory(Class elementClass) {
		Iterator<IModelerHelper> it = getFactories().iterator();
		while(it.hasNext()) {
			IModelerHelper mf = it.next();
			if(mf.isFactoryFor(elementClass)) {
				return mf;
			}
		}
		Debug.log(new Exception("No factory for : "+elementClass));
		return null;
	}
	
	
}
